namespace IAM.Interface.Common.Context
{
    using IAM.Interface.Common;
    using IAM.Interface.Common.Abnormity;
    using IAM.Interface.Common.Encrypt;
    using IAM.Interface.Common.Providers;
    using IAM.Interface.Common.XMLResolution;
    using IAM.Interface.Model;
    using IAM.Interface.Model.Interface;
    using System;
    using System.Xml;

    public class RequestContextControl
    {
        private IAM.Interface.Common.XMLResolution.AbstractXMLResolution _abxmlresolution = null;
        private RequestContextModel _contextmodel = null;
        private IAM.Interface.Common.Encrypt.EncryptDecrypt _endecrypt = null;
        private IAM.Interface.Common.Providers.IProvider _iprovider = null;
        private IAM.Interface.Common.XmlObjConversion _xmlobjconversion = null;

        public RequestContextControl(RequestContextModel contextmodel)
        {
            this._contextmodel = contextmodel;
        }

        public object Incept(string xml)
        {
            xml = this.EncryptDecrypt.Decrypt(xml);
            XmlDocument resustXd = new XmlDocument();
            resustXd.LoadXml(xml);
            Result currentresult = new Result();
            currentresult.CurrentXmlNode = resustXd.DocumentElement.SelectSingleNode("Result");
            currentresult = (Result) currentresult.ConvertToObject();
            if (currentresult.ErrorCode.Trim() != "200")
            {
                ResponseException re = new ResponseException();
                string errmessage = "";
                if (int.Parse(currentresult.ErrorCode) == 500)
                {
                    errmessage = "服务器端错误: " + currentresult.ErrorMessage;
                }
                else
                {
                    errmessage = currentresult.ErrorMessage;
                }
                re.SetStateMessage(int.Parse(currentresult.ErrorCode), errmessage);
                throw re;
            }
            XMLResolutionMgnt xmlrm = new XMLResolutionMgnt(xml);
            xmlrm.ActionType = this.ContextModel.ActionType;
            this.AbstractXMLResolution = xmlrm.CreateXMLResolution();
            XmlNode xn = this.AbstractXMLResolution.GetCurrentNode();
            this.XmlObjConversion = new IAM.Interface.Common.XmlObjConversion(xmlrm.ActionType, this.AbstractXMLResolution.XmlDocument.DocumentElement.Name);
            this.XmlObjConversion.CurrentXmlNode = xn;
            return this.XmlObjConversion.ConvertToObj();
        }

        public string Send()
        {
            string xml = "";
            this.XmlObjConversion = new IAM.Interface.Common.XmlObjConversion(this.ContextModel.ReturnType, (IConvertXml) this.ContextModel.Obj);
            xml = this.XmlObjConversion.GetXml();
            this.AbstractXMLResolution = new Request(this.ContextModel.ActionType, this.ContextModel.OperationType, this.ContextModel.TranPar, xml);
            xml = this.AbstractXMLResolution.GetWholeXML();
            this.IProvider = new ProviderMgnt(this.ContextModel.TranPar, xml).CreateProvider();
            XMlSign xs = new XMlSign();
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.LoadXml(xml);
            this.IProvider.Sign = xs.SignXml(xmlDoc);
            this.IProvider.EncryptDecrypt.Key = this.ContextModel.TranPar.Key;
            this.IProvider.Sign = this.IProvider.EncryptDecrypt.Encrypt(this.IProvider.Sign);
            this.IProvider.EncryptDecrypt.Key = this.ContextModel.TranPar.Key;
            this.IProvider.XML = this.IProvider.EncryptDecrypt.Encrypt(this.IProvider.XML);
            this.EncryptDecrypt = this.IProvider.EncryptDecrypt;
            return this.IProvider.GetResponseStr();
        }

        public IAM.Interface.Common.XMLResolution.AbstractXMLResolution AbstractXMLResolution
        {
            get
            {
                return this._abxmlresolution;
            }
            set
            {
                this._abxmlresolution = value;
            }
        }

        public RequestContextModel ContextModel
        {
            get
            {
                return this._contextmodel;
            }
        }

        public IAM.Interface.Common.Encrypt.EncryptDecrypt EncryptDecrypt
        {
            get
            {
                return this._endecrypt;
            }
            set
            {
                this._endecrypt = value;
            }
        }

        public IAM.Interface.Common.Providers.IProvider IProvider
        {
            get
            {
                return this._iprovider;
            }
            set
            {
                this._iprovider = value;
            }
        }

        public IAM.Interface.Common.XmlObjConversion XmlObjConversion
        {
            get
            {
                return this._xmlobjconversion;
            }
            set
            {
                this._xmlobjconversion = value;
            }
        }
    }
}
